#include "SipSubscribeSelector.hpp"

namespace wtsip{

CSipSubscribeSelector::CSipSubscribeSelector(CSipGDService& srv):
m_service(srv)
{}


CSipSubscribeSelector::~CSipSubscribeSelector(){}



bool CSipSubscribeSelector::insertMap(SipMessage& callerReq, SipMessage& calleeReq)
{
    if (calleeReq.isRequest() && callerReq.isRequest())
    {
        const resip::Data& calleeId = calleeReq.getTransactionId();
        const resip::Data& callerId = callerReq.getTransactionId();
        insertMap(callerId, calleeId);
    }
    return true;
}


bool CSipSubscribeSelector::insertMap(const resip::Data& callerId, const resip::Data& calleeId)
{
    m_TransMap.insert(std::pair<resip::Data, resip::Data>(callerId, calleeId));
    return true;
}


const resip::Data& CSipSubscribeSelector::getCalleeID(const resip::Data& callerId)
{
    std::map<resip::Data, resip::Data>::iterator it;
    it = m_TransMap.find(callerId);
    if (it != m_TransMap.end()){
        resip::Data& newId = it->second;
        return newId;
    }
    return resip::Data::Empty;
}


bool CSipSubscribeSelector::removeTidMap(const resip::Data& callerId)
{
    std::map<resip::Data, resip::Data>::iterator it;
    it = m_TransMap.find(callerId);
    if (it != m_TransMap.end()){
        m_TransMap.erase(it);
        return true;
    }
    return false;
}


///////////////////  Session Handles /////////////////////

//virtual bool CSipB2BSubscribe::onSuccess(SipMessage& msg, CSipSessionBase& sb)
//{
//    if (msg.isResponse())
//    {
//        resip::Data& id = msg.getTransactionId();
//        resip::Data& nextId = getCalleeID(id);
//        if (nextId != resip::Data::Empty)
//        {
//            CSipSessionBase& nextSb = (&sb != m_callee)? m_callee: m_caller;
//            CSipSubscribeSession* wkSb = dynamic_cast<CSipSubscribeSession*>(nextSb);
//            if (wkSb)
//            {
//                std::string body;
//                if (msg.getContents())
//                {
//                    body.append(msg.getContents()->getBodyData().c_str());
//                }
//                wkSb->replyOK(nextId, body);
//            }
//        }
//    }
//    return true;
//}
//
//
//bool CSipB2BSubscribe::onFailed(SipMessage& msg, CSipSessionBase& sb)
//{
//    if (msg.isResponse())
//    {
//        resip::Data& id = msg.getTransactionId();
//        resip::Data& nextId = getCalleeID(id);
//        if (nextId != resip::Data::Empty)
//        {
//            CSipSessionBase& nextSb = (&sb != m_callee)? m_callee: m_caller;
//            CSipSubscribeSession* wkSb = dynamic_cast<CSipSubscribeSession*>(nextSb);
//            if (wkSb)
//            {
//                int code = msg.header(h_StatusLine).responseCode();
//                wkSb->replyFailed(nextId, CSipSessionBase::getReasonbyCode(code));
//            }
//        }
//    }
//    return true;
//}
//
//
//bool CSipB2BSubscribe::onTrying(SipMessage& msg, CSipSessionBase& sb){
//    return true;
//}
//
//
//bool CSipB2BSubscribe::onDestroy(CSipSessionBase& sb){
//    delete this;
//}
//
//
//bool CSipB2BSubscribe::onSubscribe(SipMessage& msg, CSipSubscribeSession& subs)
//{
//    if (m_handle && !m_handle->canB2BRequest(msg, subs, *this))
//    {
//        return true;
//    } else{
//        CSipSessionBase& nextSb = (&subs != m_callee)? m_callee: m_caller;
//        CSipSubscribeSession* wkSb = dynamic_cast<CSipSubscribeSession*>(nextSb);
//        if (wkSb){
//            std::string body(msg.getContents()->getBodyData().c_str());
//            resip::Data calleeId = msg.getTransactionId();
//            resip::Data callerId = wkSb->subscribe(body);
//        }
//    }
//    return true;
//}
//
//
//bool CSipB2BSubscribe::onNotify(SipMessage& msg, CSipSubscribeSession& subs)
//{
//    if (m_handle && !m_handle->canB2BRequest(msg, subs, *this))
//    {
//        return true;
//    } else{
//        CSipSessionBase& nextSb = (&subs != m_callee)? m_callee: m_caller;
//        CSipSubscribeSession* wkSb = dynamic_cast<CSipSubscribeSession*>(nextSb);
//        if (wkSb){
//            std::string body(msg.getContents()->getBodyData().c_str());
//            resip::Data calleeId = msg.getTransactionId();
//            resip::Data callerId = wkSb->notify(body);
//        }
//    }
//    return true;
//}



}//namespace
